package qdu.edu.com.fushanf4.service.count.usertype.count;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
import qdu.edu.com.fushanf4.utils.UtilConstants;

import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class CharacterOfProductDescriptionCountMapper extends Mapper<LongWritable, Text, Text, IntWritable>{
    private final Text keyOut = new Text();
    private final static IntWritable valueOut = new IntWritable(1);

    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        // 获取一行
        String line  = value.toString();

        // 处理
        // 正则表达式过滤
        Pattern pattern = Pattern.compile(UtilConstants.RegexOfString.EXCEPT_CHINESE_CHARACTERS);
        Matcher matcher = pattern.matcher(line);
        line = matcher.replaceAll("");

        // 切分数据
        String[] characters = line.split("");

        // 输出
        for (String character : characters) {
            keyOut.set(character);
            context.write(keyOut, valueOut);
        }
    }
}
